
using Youz.Common.DBUtility;
using Youz.Common.Entity;
using System;
using System.Data;
using System.Data.SqlClient;
using System.Text;

namespace Youz.Common.DAL
{
    /// <summary>
    /// Right_RoleRight Data Class
    /// </summary>
    public partial class Right_RoleRightDAL : BaseDAL
    {
        private DbHelperSQL DbHelperSQL = new DbHelperSQL();
        public string TableName = "Right_RoleRight";

        public Right_RoleRightDAL()
        { }

        #region  成员方法
        /// <summary>
        ///  新增一条数据
        /// </summary>
        public bool Add(Right_RoleRight model)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append("insert into [Right_RoleRight](");
            strSql.Append("[RRID],[RoleID],[MenuID]");
            strSql.Append(") values (");
            strSql.Append("@RRID,@RoleID,@MenuID");
            strSql.Append(")");

            SqlParameter[] parameters = {
                  
                    new SqlParameter("@RRID", model.RRID),
                    new SqlParameter("@RoleID", model.RoleID),
                    new SqlParameter("@MenuID", model.MenuID)
            };

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        ///  保存一条数据
        /// </summary>
        public bool Update(Right_RoleRight model)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append("update [Right_RoleRight] set ");
            strSql.Append("[RoleID]=@RoleID,[MenuID]=@MenuID");
            strSql.Append(" where [RRID]=@RRID");

            SqlParameter[] parameters = {
                  
                    new SqlParameter("@RRID", model.RRID),
                    new SqlParameter("@RoleID", model.RoleID),
                    new SqlParameter("@MenuID", model.MenuID)
            };

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        ///</summary>
        public bool Delete(Guid RRID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from [Right_RoleRight] ");
            strSql.Append(" where [RRID]=@RRID");

            SqlParameter[] parameters = {
			
                new SqlParameter("@RRID", RRID)             
              
            };

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Right_RoleRight Get(Guid RRID)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append("select  top 1 * from [Right_RoleRight] ");
            strSql.Append(" where [RRID]=@RRID");

            SqlParameter[] parameters = {
			
                new SqlParameter("RRID", RRID)             
              
            };

            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return Get(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Right_RoleRight Get(DataRow row)
        {
            if (row == null) return null;

            Right_RoleRight model = new Right_RoleRight();


            if (row["RRID"] != DBNull.Value)
            {
                model.RRID = (Guid)row["RRID"];
            }

            if (row["RoleID"] != DBNull.Value)
            {
                model.RoleID = (Guid)row["RoleID"];
            }

            if (row["MenuID"] != DBNull.Value)
            {
                model.MenuID = (Int32)row["MenuID"];
            }


            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet Gets(string sqlWhere)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append("select * from [Right_RoleRight] ");
            if (sqlWhere.Trim() != "")
            {
                strSql.Append(" where " + sqlWhere);
            }

            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得前几行数据
        /// </summary>
        public DataSet Gets(int Top, string sqlWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" *  FROM [Right_RoleRight] ");

            if (sqlWhere.Trim() != "")
            {
                strSql.Append(" where " + sqlWhere);
            }
            if (filedOrder.Trim() != "")
            {
                strSql.Append(" order by " + filedOrder);
            }

            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string sqlWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM [Right_RoleRight] ");
            if (sqlWhere.Trim() != "")
            {
                strSql.Append(" where " + sqlWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }

        /// <summary>
        /// 获得数据列表分页
        /// </summary>
        public DataTable Gets(string sqlWhere, int pageindex, int pagesize, out int total, params SqlParameter[] cmdParms)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append(" SELECT * FROM [Sys_Roleinfo]  ");
            if (sqlWhere.Trim() != "")
            {
                strSql.Append(" where " + sqlWhere);
            }

            int count = 0;
            DataTable dt = DbHelperSQL.GetPager(pageindex, pagesize, "" + strSql + "", "order by RoleID desc ", out count, cmdParms);
            total = count;
            return dt;
        }
        #endregion  成员方法

        public bool Add(string menuInfos, Guid role)
        {

            SqlParameter[] parms = new SqlParameter[] {
				new SqlParameter("menuinfostr", menuInfos),
                new SqlParameter("role",role)
			};

            int rows = 0;
            DbHelperSQL.RunProcedure("up_AddRoleRight", parms, out rows);

            return rows != -1;
        }

        public DataTable GetsByMenuURL()
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append("select b.RoleID,c.MenuID,c.MenuURL from [dbo].[Right_RoleRight] b join [dbo].[Right_Menu] c on c.MenuID=b.MenuID order by b.RoleID");

            DataSet ds = DbHelperSQL.Query(strSql.ToString());
            if (ds == null || ds.Tables.Count == 0)
            {
                return null;
            }

            return ds.Tables[0];
        }
    }
}


  